package wh.FishingPhilosopher.Components;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.PointF;
import wh.FishingPhilosopher.R;
import wh.FishingPhilosopher.GameParams.GameMathAlgorithm;
import wh.GameEngine.GameObject;

public class ExternLoadAutoMoveComponent implements AutoMoveComponent{
	Context context;
	ArrayList<String> strTime= new ArrayList<String>();
	ArrayList<String> strAngle= new ArrayList<String>();
	private int timeIndex = 0;
	private int angleIndex = 0;
	private int timeflag = 0;
	private float fishMovingSpeed;
	public ExternLoadAutoMoveComponent(Context context,float fishMovingSpeed ) {
		// TODO Auto-generated constructor stub
		this.context=context;
		this.fishMovingSpeed=fishMovingSpeed;

		XmlResourceParser xrp = context.getResources().getXml(R.xml.fishroute);
		int eventType;
		try {
			eventType = xrp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) 
			{
				if (eventType == XmlPullParser.START_TAG&&xrp.getName().equals("Time")) 
				{
					eventType = xrp.next();
					strTime.add(xrp.getText());
				} 
				else if (eventType == XmlPullParser.START_TAG&&xrp.getName().equals("Angle")) 
				{
					eventType = xrp.next();
					strAngle.add(xrp.getText());
				}
				else
					eventType = xrp.next();
			}
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		timeflag = Integer.parseInt(strTime.get(timeIndex))*5;
	}
	
	@Override
	public void autoMove(GameObject gameObject) {
		// TODO Auto-generated method stub
		PointF loadPosition = null;
		float loadAngle;
		
		if(timeflag>0)
		{	
			loadAngle = Integer.parseInt(strAngle.get(angleIndex));
			loadPosition=GameMathAlgorithm.calPositionAwayOfCenter(gameObject.getCenterPosition(), fishMovingSpeed, loadAngle);
			gameObject.setBitmapAngle(loadAngle);
			gameObject.setCenterPosition(loadPosition);
			timeflag--;
			System.out.println("axiu1");
		}
		else if(timeflag==0&&angleIndex<strAngle.size()-1&&timeIndex<strTime.size()-1)
		{
			angleIndex++;
			timeIndex++;
			timeflag = Integer.parseInt(strTime.get(timeIndex))*5;
			System.out.println("axiu2");
		}
		else
		{
			angleIndex = 0;
			timeIndex = 0;
			timeflag = Integer.parseInt(strTime.get(timeIndex))*5;
			System.out.println("axiu");
		}
	}

}
